package com.example.dryyzx.controller;

import com.example.dryyzx.dao.BedRecordMapper;
import com.example.dryyzx.entity.BedRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class BedRecordController {

    @Autowired
    private BedRecordMapper bedRecordMapper;

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public boolean insertNewRecordByCustomer_idAndBed_id(int bed_id,int customer_id,String occupied_time)  {
        BedRecord bedRecord = new BedRecord();
        bedRecord.setBedId(bed_id);
        bedRecord.setCustomerId(customer_id);
        try{
            bedRecord.setOccupiedTime(sdf.parse(occupied_time));
        }catch (ParseException e){
            System.out.println("bedRecord表 传记录时，时间错误");
            return false;
        }
        bedRecord.setEnable(true);
        bedRecordMapper.insertSelective(bedRecord);
        return true;
    }

    public boolean deleteOldRecordByCustomer_idAndBed_id(int customer_id,int bed_id,Date leaving_time) {
        BedRecord bedRecord = bedRecordMapper.selectByBed_idAndCustomer_id(bed_id,customer_id);
        bedRecord.setLeavingTime(leaving_time);
        bedRecord.setEnable(false);
        bedRecordMapper.updateByPrimaryKeySelective(bedRecord);
        return true;
    }

}
